// 云函数入口文件
const cloud = require('wx-server-sdk')
const got = require('got')
const xml2js = require('xml2js')
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {

  var parseString = xml2js.parseString

  let unionId = event.unionId
  let goodsIds = event.goodsIds
  let typeStr = event.type
  let signMethod = event.signMethod
  let timestamp = event.timestamp
  let version = event.version
  let sign = event.sign
  let v = event.v

  let url = 'http://cps.kaola.com/cps/api/queryGoodsDetail?'
  let params = `unionId=${unionId}&goodsIds=${goodsIds}&type=${typeStr}&signMethod=${signMethod}&timestamp=${timestamp}&version=${version}&sign=${sign}&v=${v}`
  let urlHttp = url + params

  let getRes = await got(urlHttp)
  let resJson = {}

  parseString(getRes.body, function (err, result) {
    resJson = result
  })

  return resJson
}